﻿using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.DrawingCore.Drawing2D;
using System.DrawingCore.Imaging;
using System.IO;
using System.Text;

namespace BCService.UploadHandleComponent.ImageHandle
{
    /// <summary>
    /// 图片加工
    /// </summary>
    public class ImageProcess
    {
        /// <summary>
        /// 创建图片缩略图
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="fileSuffix">文件后缀</param>
        /// <param name="thumbnailWidth">缩略图宽度</param>
        /// <param name="thumbnailHeight">缩略图高度</param>
        /// <param name="sourceImage">原图片</param>
        /// <param name="directoryPath">原文件目录路径</param>
        /// <param name="newFilePath">缩略图文件路径</param>
        /// <returns></returns>
        public Image CreateImageThumbnail(string fileName, string fileSuffix, int thumbnailWidth, int thumbnailHeight, Image sourceImage, string directoryPath,out string newFilePath)
        {
            if (sourceImage == null)
            {
                throw new ArgumentException("原图不允许为空。");
            }

            // 源图宽度及高度 
            int imageFromWidth = sourceImage.Width;
            int imageFromHeight = sourceImage.Height;
            newFilePath = Path.Combine(directoryPath, $"{fileName}_{thumbnailWidth}x{thumbnailHeight}{fileSuffix}");
            // 生成的缩略图实际宽度及高度.如果指定的高和宽比原图大，则返回原图；否则按照指定高宽生成图片
            if (thumbnailWidth >= imageFromWidth && thumbnailHeight >= imageFromHeight)
            {
                sourceImage.Save(newFilePath);
                return sourceImage;
            }
            else
            {
                // 生成的缩略图在上述"画布"上的位置
                int X = 0;
                int Y = 0;

                // 创建画布
                Bitmap bmp = new Bitmap(thumbnailWidth, thumbnailHeight, PixelFormat.Format24bppRgb);
                try
                {
                    bmp.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
                }
                catch
                {
                    bmp.SetResolution(sourceImage.Width, sourceImage.Height);
                }
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    // 用白色清空 
                    g.Clear(Color.White);

                    // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。 
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    // 指定高质量、低速度呈现。 
                    g.SmoothingMode = SmoothingMode.HighQuality;

                    // 在指定位置并且按指定大小绘制指定的 Image 的指定部分。 
                    g.DrawImage(sourceImage, new Rectangle(X, Y, thumbnailWidth, thumbnailHeight), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel);

                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }
                    //将图片以指定的格式保存到到指定的位置
                    bmp.Save(newFilePath);
                        
                    return bmp;
                }
            }
        }
    }
}
